package com.nmf.pc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nmf.dao.gen.BizCmsArticleDataMapper;
import com.nmf.dao.gen.bean.BizCmsArticle;
import com.nmf.dao.gen.bean.BizCmsArticleData;
import com.nmf.dao.gen.bean.BizCmsCategory;
import com.nmf.pc.vo.ReturnResult;
import com.nmf.site.modules.cms.entity.Article;
import com.nmf.site.modules.cms.entity.Category;
import com.nmf.site.modules.cms.service.ArticleService;
import com.nmf.site.modules.cms.service.CategoryService;

@Controller
@RequestMapping(value = "/pc")
public class PcCmsController {
	@Autowired
	CategoryService categoryService;
	@Autowired
	ArticleService articleService;
	/**
	 * 获取分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "p/cms/category" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<BizCmsCategory,List<BizCmsArticle>> category(HttpServletRequest request, HttpServletResponse response) {
		Map<BizCmsCategory,List<BizCmsArticle>> amap = new HashMap<>();
		List<Category> categoryList = categoryService.findByParentId("CA0003994256973", null);
		for(Category category : categoryList) {
			List<Article>  alist = articleService.queryArticleList(category.getId());
			if(alist.size()>8) {
				alist = alist.subList(0, 8);
			}
			
			BizCmsCategory bizCmsCategory = new BizCmsCategory();
			BeanUtils.copyProperties(category, bizCmsCategory);
			
			List<BizCmsArticle> clist = new ArrayList<BizCmsArticle>();
			for(Article article : alist) {
				BizCmsArticle bizCmsArticle = new BizCmsArticle();
				BeanUtils.copyProperties(article, bizCmsArticle);
				clist.add(bizCmsArticle);
			}
			
			amap.put(bizCmsCategory, clist);
		}
		return amap;
	}
	@Autowired
	BizCmsArticleDataMapper bizCmsArticleDataMapper;
	
	@RequestMapping(value = { "p/cms/content" }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ReturnResult cmsContent(String articleId,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizCmsArticleData bizCmsArticleData = bizCmsArticleDataMapper.selectByPrimaryKey(articleId);
		returnResult.setData(bizCmsArticleData);
		return returnResult;
	}
}
